package api

import (
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"hcy-api/lib/http"
	"hcy-api/structs/vap"
	http2 "net/http"
	"vap/module/service"
)

var GuaApi = new(guaApi)

type guaApi struct {
}

func (api guaApi) Conn(c *gin.Context) {
	var in vap.OpenAppDesktopInput
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	var UpGrader = websocket.Upgrader{
		CheckOrigin: func(r *http2.Request) bool {
			return true
		},
		// 在原始websocket中添加对guacamole协议的支持
		Subprotocols: []string{"guacamole"},
	}
	ws, err := UpGrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	err = service.GuaServ.Conn(in, ws)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
}

func (api guaApi) Monitor(c *gin.Context) {
	in := struct {
		Sid int64 `form:"sid,string"`
	}{}
	if err := c.ShouldBindQuery(in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	var UpGrader = websocket.Upgrader{
		CheckOrigin: func(r *http2.Request) bool {
			return true
		},
		// 在原始websocket中添加对guacamole协议的支持
		Subprotocols: []string{"guacamole"},
	}
	ws, err := UpGrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	err = service.GuaServ.ConnForMonitor(in.Sid, ws)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}

}

func (api guaApi) SH(c *gin.Context) {
	var in vap.ShArgus
	if err := c.ShouldBindQuery(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	var UpGrader = websocket.Upgrader{
		CheckOrigin: func(r *http2.Request) bool {
			return true
		},
		// 在原始websocket中添加对guacamole协议的支持
		Subprotocols: []string{"guacamole"},
	}
	ws, err := UpGrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
	err = service.GuaServ.Sh(in, ws)
	if err != nil {
		http.BadWithErr(c, err)
		return
	}
}
